/* 
 * @(#)ForumAprioriAction.java    Created on Apr 18, 2013
 * Copyright (c) 2013 ZDSoft Networks, Inc. All rights reserved.
 * $Id$
 */
package com.leruan.aasimp.action.forum;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.leruan.aasimp.entity.AprioriEntity;
import com.leruan.aasimp.entity.Forumstst;
import com.leruan.aasimp.entitydto.BrandMap;
import com.leruan.aasimp.service.MainPostService;
import com.leruan.aasimp.util.Apriori;
import com.opensymphony.xwork2.ActionSupport;

public class ForumAprioriAction extends ActionSupport {

    private static final long serialVersionUID = -748836572679016213L;

    @Resource
    private MainPostService mainPostService;

    public String showAprioriTable() {
        HttpServletRequest request = ServletActionContext.getRequest();
        List<Forumstst> forumstats = mainPostService.getForumststs();
        List<String> transSet = new ArrayList<String>();
        for (int i = 0; i < forumstats.size(); i++) {
            String brands = forumstats.get(i).getBrands();
            transSet.add(brands);
        }
        Map<String, Double> map = BrandMap.getWeightMap();
        String[] transSet1 = new String[transSet.size()];
        for (int j = 0; j < transSet.size(); j++) {
            transSet1[j] = transSet.get(j);
        }
        Apriori aa = new Apriori(transSet1);
        List<AprioriEntity> allapriori = aa.run(0.9, 0.3);
        for (int k = 0; k < allapriori.size(); k++) {
            String describe = allapriori.get(k).getDescribe();
            String[] split = describe.split("-->");
            List<String> list1 = new ArrayList<String>();
            for (int i = 0; i < split[0].length(); i++) {
                list1.add(split[0].substring(i, i + 1));
            }
            for (int i = 0; i < split[1].length(); i++) {
                list1.add(split[1].substring(i, i + 1));
            }
            // 计算权系数
            double s = 0;
            for (int i = 0; i < list1.size(); i++) {
                s += map.get(list1.get(i));
            }
            double weight = s / list1.size();
            allapriori.get(k).setWeightConfidence(weight * allapriori.get(k).getConfidence());
            Double count = 0.0;
            for (int i = 0; i < forumstats.size(); i++) {
                String brands = forumstats.get(i).getBrands();
                boolean flag = true;
                for (int j = 0; j < list1.size(); j++) {
                    if (!brands.contains(list1.get(j))) {
                        flag = false;
                    }
                }
                if (flag) {
                    count++;
                }
            }
            allapriori.get(k).setSupport(count / forumstats.size());
            allapriori.get(k).setWeightSupport(weight * allapriori.get(k).getSupport());
        }
        request.getSession().setAttribute("allapriori", allapriori);
        return "success";
    }

    public MainPostService getMainPostService() {
        return mainPostService;
    }

    public void setMainPostService(MainPostService mainPostService) {
        this.mainPostService = mainPostService;
    }

}
